const tool = {
    getSearchData: () => {
        let search = decodeURIComponent(location.search);
        let itemArr = search.slice(1).split("&");
        let obj = {}
        itemArr.forEach((item) => {
            let arr = item.split("=");
            obj[arr[0]] = arr[1];
        });
        return obj;
    },

    getCookie: (name) => {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            return arr[2];
        } else {
            return null;
        }
    },

    delCookie: (name) => {
        console.log(name)
        const exp = new Date();
        exp.setTime(exp.getTime() - 1);
        const cval = tool.getCookie(name);
        console.log(cval)
        if (cval != null) {
            document.cookie = name + "=" + escape(cval) + ";expires=" + exp.toGMTString() + ";path=/; domain=pcauto.com.cn";
        }
    },

    ready: (callback) => {
        if (window.AlipayJSBridge) {
            callback && callback();
        } else {
            // 如果没有注入则监听注入的事件
            document.addEventListener('AlipayJSBridgeReady', callback, false);
        }
    },

    location: () => {
        console.log('经过定位')
        return new Promise((resolve, reject) => {
            Locate.init(
                {
                    bDisableIp: true,
                    bDebug: true,
                    sDebugType: "alert",
                    bCookie: true,
                    nCookieTime: 864 * 1e5,
                    sCookieName: "wap0712222",
                    nH5Timeout: 60000,
                },
                (data) => {
                    console.log(data)
                    if (data.dataType === "default") {
                        reject({ message: "获取不到定位，请打开定位" });
                    } else {
                        resolve(data.data);
                    }
                }
            );
        });
    },

    // 去掉名称中的'省'或'市'，使用插件location，直辖市返回的省市名都是‘xxx市’【注意】
    formatArea: (area) => {
        return area.replace(/^(.+)省$/, "$1").replace(/^(.+)市$/, "$1");
    }

}

export default tool;
